//
// Created by liushuai on 2021/2/15.
//

#include "thirdMax.h"
int thirdMax(vector<int>& nums) {
    if(nums.size()<3)
        return nums[nums.size()-1];
    int size = nums.size();
    int prefix = INT_MAX;

    int count = 0;
    int last = INT_MIN;
    for(int i=0;i<size;i++){
        for(int j=i;j<size;j++){
            if(nums[j]>=nums[i])
                std::swap(nums[j],nums[i]);
        }
        if(nums[i]>last)
            last = nums[i];
        if(nums[i]<prefix){
            count+=1;
            prefix = nums[i];
        }
        if(count == 3)
            break;
    }
    if(count == 3)
        return prefix;
    else
        return last;
}